package net.sinmetal.twitcockpit.twitter;

import java.util.List;

import net.sinmetal.twitcockpit.exception.TwitterRuntimeException;
import twitter4j.Query;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

public class Twitter4tc {
    /** Twitter */
    private Twitter twitter;

    @SuppressWarnings("unused")
    private Twitter4tc() {
    };

    /**
     * コンストラクタ
     *
     * @param consumerKey
     * @param consumerSecret
     */
    public Twitter4tc(String consumerKey, String consumerSecret) {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(consumerKey);
        builder.setOAuthConsumerSecret(consumerSecret);
        builder.setGZIPEnabled(false);
        OAuthAuthorization auth = new OAuthAuthorization(builder.build());

        twitter = new TwitterFactory().getInstance(auth);
    }

    /**
     * コンストラクタ
     *
     * @param accessToken
     */
    public Twitter4tc(String consumerKey, String consumerSecret,
            AccessToken accessToken) {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(consumerKey);
        builder.setOAuthConsumerSecret(consumerSecret);
        builder.setGZIPEnabled(false);
        OAuthAuthorization auth = new OAuthAuthorization(builder.build());

        twitter = new TwitterFactory().getInstance(auth);
        twitter.setOAuthAccessToken(accessToken);
    }

    /**
     * アクセストークン取得
     *
     * @return
     */
    public AccessToken getOAuthAccessToken() {
        try {
            return twitter.getOAuthAccessToken();
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * アクセストークン取得
     *
     * @param requestToken
     * @return
     */
    public AccessToken getOAuthAccessToken(RequestToken requestToken,
            String oauthVerifier) {
        try {
            return twitter.getOAuthAccessToken(requestToken, oauthVerifier);
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * User取得
     *
     * @return
     */
    public User getUser() {
        try {
            return twitter.verifyCredentials();
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * リクエストトークン作成
     *
     * @return
     */
    public RequestToken getOAuthRequestToken(String callbackURL) {
        try {
            return twitter.getOAuthRequestToken(callbackURL);
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * ホームタイムライン取得
     *
     * @return
     */
    public ResponseList<Status> getHomeTimeline() {
        try {
            return twitter.getHomeTimeline();
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * Mentions取得
     *
     * @return
     */
    public ResponseList<Status> getMentions() {
        try {
            return twitter.getMentions();
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * Search
     *
     * @param query
     * @return
     */
    public List<Tweet> search(Query query) {
        try {
            return twitter.search(query).getTweets();
        } catch (TwitterException e) {
            throw new TwitterRuntimeException();
        }
    }

    /**
     * ツイートする
     *
     * @param tweet
     * @return
     */
    public Status updateStatus(String status) {
        try {
            return twitter.updateStatus(status);
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }
}
